﻿using System;
using System.Collections.Generic;

namespace FastJSON
{
    public static class PHP_DateTimeTransform
    {
        private static DateTime DateTimeSpan = new DateTime(1970, 1, 1, 0, 0, 0);

        public static DateTime PHP_TS2DT(TimeSpan ts)
        {
            return PHP_TS2DT(ts.Ticks);
        }

        public static DateTime PHP_TS2DT(long ts)
        {
            return new DateTime(DateTimeSpan.Ticks + ts * 10000000);
        }

        public static TimeSpan PHP_DT2TS(DateTime dt)
        {
            return new TimeSpan(PHP_DT2LN(dt));
        }

        public static long PHP_DT2LN(DateTime dt)
        {
            return (dt - DateTimeSpan).Ticks / 10000000;
        }

        public static string PHP_DT2LN(string dt)
        {
            if (string.IsNullOrEmpty(dt))
                return dt;

            DateTime d;
            if (DateTime.TryParse(dt, out d))
            {
                return PHP_DT2LN(d).ToString();
            }
            return null;
        }
    }
}